package org.jeecg.modules.paycenter.channel.hundsun;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.paycenter.enums.PaySourceEnum;
import org.jeecg.modules.paycenter.service.BasePayChannelService;
import org.jeecg.modules.paycenter.service.IPayChannelService;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBill;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HundSunPayChannelService extends BasePayChannelService implements IPayChannelService {
    public static  final String CHANNEL = PayChannelEnum.HUNDSUN.channelType;
    @Autowired private IHyCustomerWalletService customerWalletService;
    @Autowired private HundSunPayService hundSunPayService;
    @Autowired private HundSunUtil hundSunUtil;

    //region base
    @Override
    public PayChannelEnum getChannelEnum() {
        return PayChannelEnum.HUNDSUN;
    }

    @Override
    public boolean checkChannel() {
        return HyCfgUtil.isUsed2("hundsun");
    }

    @Override
    public String getChannel() {
        return PayChannelEnum.HUNDSUN.channelType;
    }
    //endregion

    //region 电子回单

    @Override
    public Result<?> batchDownloadCustomerBilHd(List<HyCustomerBill> customerBills){
        return Result.error("操作不可用");
    }

    @Override
    public Result<?> batchDownloadShippingBillHd(List<HyShippingNoteBill> shippingNoteBills) {
        List<String> ids = new ArrayList<>();
        for (HyShippingNoteBill hyShippingNoteBill : shippingNoteBills) {
            if (!CHANNEL.equals(hyShippingNoteBill.getPayChannel())) {
                continue;
            }
            if (hyShippingNoteBill.getBankHd() != null) {
                continue;
            }
            ids.add(hyShippingNoteBill.getId());
        }
        if (ids.size() == 0) {
            return Result.error("无需要下载的数据");
        }
        //查询回单
        try {
            return hundSunUtil.batchDownloadShippingBillHd(ids);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
    //endregion


    //region 支付

    @Override
    @Transactional
    public Result<?> billPayBatch(List<HyBill> hyBills, HyCustomer customer) {
        HyCustomerWallet customerWallet = customerWalletService.getByCustomerIdForUpdate(customer.getId());
        if (customerWallet == null) {
            return Result.error("货主系统钱包不存在");
        }
        if (customerWallet.izFrozen()) {
            return Result.error("货主系统钱包已冻结");
        }
        BigDecimal totalAmt = billService.getBillCustomerTotalFee(hyBills);
        //货主系统钱包余额
        if (!customerWallet.izEnoughAmt(totalAmt)) {
            return Result.error("系统钱包余额不足");
        }
        List<HyShippingNote> hyShippingNotes = billService.getBillShippingList(hyBills);
        Map<String, HyDriver> driverMaps = billService.getDriverMaps(hyShippingNotes); //司机
        Map<String, HyDriverBank> driverBankMaps = billService.getDriverBankMaps(hyShippingNotes);
        //检查司机银行卡
        for (HyShippingNote hyShippingNote : hyShippingNotes) {
            if(!"2".equals(hyShippingNote.getPayType())){
                String driverBankId = hyShippingNote.getDriverBankId();
                if(StringUtils.isEmpty(driverBankId)){
                    return Result.error(String.format("运单%s:未设置司机银行卡,不能支付",hyShippingNote.getShippingNoteNumber()));
                }
                if(!driverBankMaps.containsKey(driverBankId)){
                    return Result.error(String.format("运单%s:司机银行卡不存在,不能支付",hyShippingNote.getShippingNoteNumber()));
                }
            }
        }

        Map<String, HyVehicle> vehicleMaps = billService.getVehicleMaps(hyShippingNotes);//车辆
        Map<String, HyCarrier> carrierMaps = billService.getCarrierMaps(hyShippingNotes);//车队长

        Map<String, HyShippingNote> shippingNoteMap = billService.shippingListToMap(hyShippingNotes);
        for (HyBill hyBill : hyBills) {
            hyBill.setPaySource(PaySourceEnum.ACCOUNT_PAY.value);//支付源为货主账户余额
        }
        hundSunPayService.payBatch(hyBills, customer, shippingNoteMap, driverMaps, driverBankMaps, carrierMaps, vehicleMaps, totalAmt);
        this.billPayBatch(hyBills);
        return Result.OK("申请支付成功");
    }

    @Override
    public Result<?> billRePayBatch(List<HyBill> hyBills, HyCustomer customer) {
        List<HyShippingNoteBill> hyShippingNoteBills = hundSunPayService.getBillRePayDatas(hyBills);
        List<HyShippingNote> hyShippingNotes = billService.getBillShippingList(hyBills);
        Map<String, HyDriverBank> driverBankMaps = billService.getDriverBankMaps(hyShippingNotes);
        Map<String, HyCarrier> carrierMaps = billService.getCarrierMaps(hyShippingNotes);//车队长
        Map<String, HyShippingNote> shippingNoteMap = billService.shippingListToMap(hyShippingNotes);
        hundSunPayService.resetShippingBills(hyBills ,hyShippingNoteBills, shippingNoteMap, driverBankMaps,carrierMaps);
        this.billPayBatch(hyBills);
        return Result.OK("申请支付成功");
    }

    //endregion
}
